package com.c3w.synchro.member.validator;

import com.c3w.synchro.common.dao.BeanDao;
import com.c3w.synchro.common.validation.ValidationResult;
import com.c3w.synchro.common.validation.Validator;
import com.c3w.synchro.common.validation.event.ValidationFailedException;
import com.c3w.synchro.member.bean.Team;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * User: magirard
 * Date: 13-02-09
 */
@Repository("teamValidator")
public class TeamValidator implements Validator<Team> {
    @Autowired
    private BeanDao<Team> teamDao;

    @Override
    public void validate(Team bean) throws ValidationFailedException {
        ValidationResult result = new ValidationResult();

        // Verifie l'existance du bean
        if (bean != null) {

            String name = bean.getName();
            if (name == null) {
                result.add("TeamName", "Team name must be provided");
            }
        } else {
            result.add("Team", "Team doesn't exist");
        }


        if (result.getErrorCount() > 0) {
            throw new ValidationFailedException(result);
        }
    }
}
